package org.dey.serviceImpl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.commons.lang3.StringUtils;
import org.dey.entity.Result;
import org.dey.mapper.OrderSettingMapper;
import org.dey.pojo.OrderSetting;
import org.dey.service.OrderSettingService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * @author hg
 * @date 2022/3/29
 * @classname
 */
@Service
@Transactional
public class OrderSettingServiceImpl extends ServiceImpl<OrderSettingMapper, OrderSetting> implements OrderSettingService {

    @Override
    public Result szrs(OrderSetting orderSetting) {

        Result result = null;

        boolean save = false;

        String str = "";

        QueryWrapper<OrderSetting> queryWrapper = new QueryWrapper<OrderSetting>();



        String format = new SimpleDateFormat("yyyy-MM-dd").format(orderSetting.getOrderDate());

        queryWrapper.eq("orderDate", format);

        OrderSetting one = this.getOne(queryWrapper);


        if (one== null) {
            save = this.save(orderSetting);
            str = "预约设置添加成功";
        } else {
            UpdateWrapper<OrderSetting> updateWrapper = new UpdateWrapper<OrderSetting>();
            updateWrapper.eq("orderDate",format).set("number", orderSetting.getNumber());
            save = this.update(updateWrapper);

            str = "预约设置修改成功";

        }

        if (save) {
            result = new Result(true,str);
        } else {
            result = new Result(true, "预约设置失败");
        }

        return result;
    }

    @Override
    public List<OrderSetting> ordersettingqb() {

        QueryWrapper<OrderSetting> queryWrapper = new QueryWrapper<OrderSetting>();

        queryWrapper.gt("orderDate",new Date());

        List<OrderSetting> list = this.list(queryWrapper);

        list.forEach(System.out::println);
        return list;
    }
}
